package io.renren.modules.sys.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.OnshelvesService;
import io.renren.modules.sys.service.OnshelvesitemService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.service.ReplenishmentService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-03-26 18:02:31
 */
@RestController
@RequestMapping("sys/replenishment")
public class ReplenishmentController {
    @Autowired
    private ReplenishmentService replenishmentService;

    @Autowired
    private OnshelvesService onshelvesService;

    @Autowired
    private OnshelvesitemService onnshelvesitemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:replenishment:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = replenishmentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:replenishment:info")
    public R info(@PathVariable("id") Integer id){
        ReplenishmentEntity replenishment = replenishmentService.getById(id);

        return R.ok().put("replenishment", replenishment);
    }

    /**
     * 保存
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:replenishment:update")
    public R update(@RequestBody ReplenishmentEntity replenishment){
        replenishmentService.save(replenishment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:replenishment:save")
    public R save(@RequestBody String str){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        JSONObject json = JSONObject.fromObject(str);
        JSONArray arrayShelves = JSONArray.fromObject(json.get("shelvesid"));
        JSONArray arrayProduct = JSONArray.fromObject(json.get("products"));
        int count = 0;
        int price = 0;
        if(json.getInt("type") == 1){
            List<OnshelvesEntity> ids = new ArrayList<OnshelvesEntity>();
            for(int i=0;i<arrayProduct.size();i++){
                count +=arrayProduct.getJSONObject(i).getInt("count");
                price +=count*arrayProduct.getJSONObject(i).getInt("costprice");
            }
            for(int i=0;i<arrayShelves.size();i++){
                OnshelvesEntity onshelvesEntity = new OnshelvesEntity();

                onshelvesEntity.setUppernumber("SJ"+sdf.format(new Date()));
                onshelvesEntity.setShelvesid(arrayShelves.getJSONObject(i).getInt("id"));
                onshelvesEntity.setType(arrayProduct.size());
                onshelvesEntity.setCreatetime(new Date());
                onshelvesEntity.setLasttime(new Date());
                onshelvesEntity.setCreateuser(Integer.parseInt(user.getUserId().toString()));
                onshelvesEntity.setLastuser(Integer.parseInt(user.getUserId().toString()));

                onshelvesEntity.setRemark("");
                onshelvesEntity.setStatu(json.getInt("statu"));
                onshelvesEntity.setNum(count);
                onshelvesEntity.setCost(new BigDecimal(price));
                onshelvesService.save(onshelvesEntity);
                ids.add(onshelvesEntity);
            }

            for(OnshelvesEntity entity:ids){
                for(int i=0;i<arrayProduct.size();i++){
                    OnshelvesitemEntity onshelvesitemEntity = new OnshelvesitemEntity();
                    onshelvesitemEntity.setProductid(arrayProduct.getJSONObject(i).getInt("id"));
                    onshelvesitemEntity.setOncount(arrayProduct.getJSONObject(i).getInt("count"));
                    onshelvesitemEntity.setOnshelvesid(entity.getId());
                    onshelvesitemEntity.setCostprice(new BigDecimal(arrayProduct.getJSONObject(i).getInt("costprice")));
                    onshelvesitemEntity.setSellprice(new BigDecimal(arrayProduct.getJSONObject(i).getInt("sellprice")));
                    onshelvesitemEntity.setTotalcost(new BigDecimal(arrayProduct.getJSONObject(i).getInt("count")*arrayProduct.getJSONObject(i).getInt("costprice")));
                    onnshelvesitemService.save(onshelvesitemEntity);
                }
            }
        }
        ReplenishmentEntity replenishment = replenishmentService.getById(json.getInt("ids"));
        replenishment.setStatu(json.getInt("type"));
        replenishmentService.updateById(replenishment);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:replenishment:delete")
    public R delete(@RequestBody Integer[] ids){
        replenishmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
